// request
import {
  getManuStaticsData,
  getManuSpreadEffect,
  getManuHotComment,
  getManuRecently_site,
  getManuSpreadTrends,
} from '@/api/manageEnd/manuscript/manuEffectAssess.js'

export default {
  methods: {
    async getManuStaticsData(id) {
      const res = await getManuStaticsData(id)
      // console.log('getManuStaticsData', res)
      const data = res.data.data || {}
      /* data.forwardNum = 123
      data.forwardNumAll = 1222223
      data.collectNumAll = 323 */

      this.entryDataCountStat.forEach((it) => (it.num = data[it.field]))
      this.allPlatformDataCountStat.forEach((it) => (it.num = data[it.field]))
    },

    async getManuSpreadEffect(id) {
      const res = await getManuSpreadEffect(id)
      // console.log('getManuSpreadEffect', res)
      const data = res.data.data || []
      this.entryManuSpreadEffect.data = data
    },

    async getManuHotComment(id) {
      const res = await getManuHotComment(id)
      // console.log('getManuHotComment', res)
      const data = res.data.data || []
      this.hotComment.data = data
    },

    async getManuRecently_site(id, params) {
      const res = await getManuRecently_site(id, params)
      console.log('getManuRecently_site', res)
      const data = res.data.data || []

      if (!data.length) {
        this.show.hotTrend = false
        this.show.mediaContribution = false
        return
      }

      /* 媒体贡献度 */
      this.show.mediaContribution = true
      const _data = data.map((it) => ({ name: it.sitename, value: it.total }))
      _data[0].selected = true
      this.mediaContribution_chart({
        data: _data,
      })
      /* 添加点击事件 */
      const instance = this.$refs['mediaContribution'].getChartInstance()
      instance.off()
      instance.on('click', (params) => {
        // console.log('params', params)
        const { name } = params
        const v = data.find((d) => d.sitename === name)
        this.renderHotTrend_chart(v.list || [])
      })

      /* 热度趋势 */
      const hot_data = data[0].list || []
      this.renderHotTrend_chart(hot_data)
    },

    renderHotTrend_chart(data) {
      if (!Array.isArray(data) || !data.length) {
        this.show.hotTrend = false
        return
      }
      this.show.hotTrend = true
      const obj = {
        xData: [],
        data: [],
      }
      data.forEach((it) => {
        obj.xData.push(it.key)
        obj.data.push(it.value)
      })
      this.hotTrend_chart(obj)
    },

    async getManuSpreadTrends(id, params) {
      const res = await getManuSpreadTrends(id, params)
      // console.log('getManuSpreadTrends', res)
      const data = res.data.data || []
      if (!data.length) {
        this.show.spreadTrend = false
        return
      }
      this.show.spreadTrend = true
      const options = {
        xData: [],
        reshipData: [],
        readData: [],
      }
      data.forEach((it) => {
        options.xData.push(it.key)
        const value = it.value || {}
        options.reshipData.push(value.forwardNumAll)
        options.readData.push(value.readCntAll)
      })
      this.spreadTrend_chart(options)
    },
  },
}
